﻿using BLL;
using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WeiBoWeb.Controllers
{
    public class DefaultController : Controller
    {

        


        // GET: Default
        public ActionResult Index()
        {
           
            ViewBag.list = LoginManager.GetMicroBlogs();
            return View();
        }
        //简陋浏览其他用户+浏览用户——评论
        public ActionResult QiTaIndex()
        {
            return View();
        }
        //
        public ActionResult Customer()
        {
            ViewBag.conlist = DefaultManager.GetMicroBlogs();
            int Uid = Convert.ToInt32(Session["UserId"]);
            ViewBag.Info = WeiBoManager.SelectUserInfo(Uid);  //资料


            ViewBag.Follow = WeiBoManager.SelectUserFollow(Uid);//我的关注

            using (MicroBlogEntities db = new MicroBlogEntities())  //关注与粉丝的数量
            {
                //ViewBag.Count_WeiBo =db.MicroBlog.Where(m => m.U_ID== Uid).Count(c => c.M_ID);
                ViewBag.Count_WeiBo = (from m in db.MicroBlog where m.U_ID == Uid select m.M_ID).Count();
                ViewBag.Count_Follow = (from f in db.UserFollow where f.U_ID == Uid select f.FollowID).Count();
                ViewBag.Count_Fans = (from s in db.UserFans where s.U_ID == Uid select s.FansID).Count();
            }
            return View();
        }
       
        public ActionResult Tadeweibo()
        {
            return View();
        }


        //登录功能
        public ActionResult Login(string name, string pwd)
        {
            Users u = LoginManager.Login(name, pwd);
            if (u != null)
            {
                Session["UserId"] = u.U_ID;
                return Content("true");
            }
            else
            {
                return Content("false");
            }

        }
        [ValidateInput(false)]
        public ActionResult Add(string con)
        {
            MicroBlog m = new MicroBlog()
            {
                U_ID = Convert.ToInt32(Session["UserId"]),
                M_Content = con,
                M_DataTime = DateTime.Now
            };
            bool a = false;
            using (MicroBlogEntities db=new MicroBlogEntities())
            {
                db.MicroBlog.Add(m);
                if (db.SaveChanges() > 0)
                {
                    a = true;
                }
            }
            if (a)
            {
                return Content("true");
            }
            return Content("false");

        }
    }
}